package org.openlaszlo.eu.dto;

import com.thoughtworks.xstream.annotations.XStreamAlias;

/**
 * This class is used to record an exception. Once the exception is record via the constructor
 *  it can be serialized via the toXML method.
 *  
 * @author Dominique Vernier
 *
 */
@XStreamAlias("error")
public class ErrorMessageDTO extends FoundationDTO {

	/**
	 * Will construct an ErrorMessageDTO object containing the exception message and stackTrace.
	 * 
	 * @param e The exception to record.
	 */
	public ErrorMessageDTO(Exception e) {
		this.setMessage(e);
		this.setStackTrace(e);
	}
	
	/**
	 * The message of the exception.
	 */
	private String message;
	/**
	 * The stackTrace of the exception.
	 */
	private String stackTrace;

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the stackTrace
	 */
	public String getStackTrace() {
		return stackTrace;
	}

	/**
	 * @param stackTrace the stackTrace to set
	 */
	public void setStackTrace(String stackTrace) {
		this.stackTrace = stackTrace;
	}

	/**
	 * Assign the exception message to the message attribute. This method is called by the constructor.
	 * @param e
	 */
	public void setMessage(Exception e) {
		this.message = e.getMessage();
	}
	
	/**
	 * Assign the stackTrace to the stackTrace attribute. This method is called by the constructor.
	 * @param e
	 */
	public void setStackTrace(Exception e) {
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(e.getMessage());
		for (StackTraceElement s: e.getStackTrace()) {
			stringBuffer.append("\n\tat "+ s.getClassName()+"."+s.getMethodName()+"("+s.getFileName()+":"+s.getLineNumber()+")");
		}
		this.stackTrace = stringBuffer.toString();
	}	

}
